function getTreatmentUnit() {
    var treatmentUnit = $("#treatmentUnit").val();
    if (treatmentUnit.indexOf(',') < 0) {
        var html = '<a class="btn treatmentUnitName" onclick="getFirstLevelProjectByTreatmentUnit(\'' + treatmentUnit + '\')">' + treatmentUnit + '</a>';
        $("#treatmentUnitHtml").html(html);
        getFirstLevelProjectByTreatmentUnit(treatmentUnit);
    } else {
        var treatmentUnits = [];
        treatmentUnits = treatmentUnit.split(',');
        var html = '';
        for (var i = 0; i < treatmentUnits.length; i++) {
            html += '<a class="btn treatmentUnitName" onclick="getFirstLevelProjectByTreatmentUnit(\'' + treatmentUnits[i] + '\')">' + treatmentUnits[i] + '</a>';
        }
        $("#treatmentUnitHtml").html(html);
        getFirstLevelProjectByTreatmentUnit(treatmentUnits[0]);
    }
}

function getFirstLevelProjectByTreatmentUnit(treatmentUnit) {
    var customerId = $('#customerId').val();
    $("#treatmentUnit").val(treatmentUnit);
    var dc = treatmentUnit + "," + customerId;
    $.ajax({
        url: Feng.ctxPath + "/payService/getFirstLevelProject",
        type: "post",
        data: {
            "dc": dc
        },
        success: function (data) {
            var txt = '';
            for (var i = 0; i < data.length; i++) {
                txt += ' <option value="' + data[i].firstLevelProject + '">  &nbsp' + data[i].firstLevelProject + '</option>';
            }
            document.getElementById('firstLevelProjectDivsd').innerHTML = txt;
            openFirstLevelProject();
        },
        error: function (data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        }
    })
}

function openFirstLevelProject() {
    var department = $("#treatmentUnit").val();
    var customerId = $('#customerId').val();
    var firstLevelProject = $("#firstLevelProjectDivsd").val();
    var cdf = customerId + "," + department + "," + firstLevelProject;
    $.ajax({
        url: Feng.ctxPath + "/payService/getPaySeriveName",
        type: "post",
        data: {
            "cdf": cdf
        },
        success: function (data1) {
            var txts = '';
            for (var i = 0; i < data1.length; i++) {
                // if (data1[i].payServicePriceOne == data1[i].payServicePriceTwo) {
                txts += ' <div style="margin: 10px 0">  <a href=\"javascript:;\" style="font-size: 14px;" onclick=\"chooseProject(\'' + data1[i].id + '\')\">&nbsp' + data1[i].payServiceName + '&nbsp' + data1[i].payServicePriceTwo + '元</a></div>'
                // } else {
                //     txts += ' <div style="margin: 10px 0">  <a href=\"javascript:;\" style="font-size: 14px;" onclick=\"chooseProject(\'' + data1[i].id + '\')\">&nbsp' + data1[i].payServiceName + '&nbsp' + data1[i].payServicePriceOne + '-' + data1[i].payServicePriceTwo + '元</a></div>'
                // }
            }
            ;
            document.getElementById('payServiceNamesd').innerHTML = txts
        },
        error: function (data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name))
        }
    })
}

function chooseProject(id) {
    var member = $("#discountDiscount").val();
    $.ajax({
        url: Feng.ctxPath + "/consultant/getCustomerStatus/" + $("#customerId").val(),
        dataType: "json",
        contentType: "application/json",
        type: "POST",
        success: function (data) {
            if (data.split(",")[0] == '复诊') {
                alert("复诊不能重新开单,请去进行重咨操作！");
                return
            } else if (data.split(",")[2] == 3) {
                alert("患者TC不是当前登录人，请联系前台分配患者！");
                return
            } else {
                $.ajax({
                    type: "POST",
                    url: Feng.ctxPath + "/payService/projectDetail",
                    data: {
                        id: id
                    },
                    dataType: "json",
                    success: function (data) {
                        var dealList = $(".proDealInfo");
                        var isChoose = 0;
                        var doctorHtml = document.getElementById('projectDoctor').innerHTML;
                        for (var i = 0; i < dealList.length; i++) {
                            if (data.id == dealList[i].payServiceId) {
                                isChoose = 1;
                            }
                        }
                        if (isChoose == 0) {
                            if (data.firstLevelProject == '种植牙俱乐部') {
                                var html = '<div class="proDealInfo"><div id="' + data.id + 'toothBox" class="toothBox" style="display: flex;flex-direction: column;flex: 1;height: 24px;"><div style="flex: 1;display: flex;border-bottom: 1px solid #999999"><div id="' + data.id + 'toothBox1" class="leftUp" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox2" class="rightUp" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div><div style="flex: 1;display: flex"><div id="' + data.id + 'toothBox3" class="leftDown" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox4" class="rightDown" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div></div><div style="flex: 2;" class="beyond" title="' + data.payServiceName + '"><span class="payServiceName">' + data.payServiceName + '<span><input class="payServiceId" value="' + data.id + '" type="hidden" /><input class="vipPayserviceId" value="0" type="hidden" /></div><div><input autocomplete="off" disabled="disabled" oninput="decimalNum(this),updatePrice(this)" type="text" class="payService" name="payService" value="' + data.payServicePriceTwo + '"/><input class="discountHide" value="' + data.payServicePriceTwo + '" type="hidden"><input class="unitPrice" value="' + data.payServicePriceTwo + '" type="hidden"><input class="unitPriceOne" value="' + data.payServicePriceOne + '" type="hidden"/><input class="unitPriceTwo" value="' + data.payServicePriceTwo + '" type="hidden"/><input class="priceRecord" value="' + data.payServicePriceTwo + '" type="hidden"/><input class="priceDiscountRecord" value="100" type="hidden"/><input class="primary" value="' + data.payServicePriceTwo + '" type="hidden"></div><div style="flex: 0.5"><span>' + data.unit + '</span></div><div><a onclick="numReduce(this)" style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="reduce">-</a><input class="num" oninput="decimalNum(this),changeNum(this)" style="width:35px;vertical-align: top;" type="text" value="1"><a onclick="numAdd(this)" style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="add">+</a></div><div><input autocomplete="off" disabled="disabled" oninput="decimalNum(this),priceDiscount(this)" class="amount" value="' + data.payServicePriceTwo + '" type="text"/><input class="realAmount" value="' + data.payServicePriceTwo + '" type="hidden"/></div><div><input disabled="disabled" autocomplete="off" oninput="decimalNum(this),updateDiscount(this)" class="discount" value="100" type="text"/></div><div><select onchange="updateDoctor(this)" class="doctor" style="width: 90%;">' + doctorHtml + '</select></div><div style="flex: 0.5"><a class="deleteDeal" onclick="deleteProject(this)">删除</a></div></div>';
                                $("#projectListInfo").append(html);
                            } else {
                                if (data.remarks == "a") {
                                    var discount = 100 * member;
                                    var payServicePriceTwo = parseFloat(data.payServicePriceTwo).toFixed(1);
                                    var payServicePriceT = parseFloat(payServicePriceTwo / member).toFixed(1);
                                    var html = '<div class="proDealInfo"><div id="' + data.id + 'toothBox" class="toothBox" style="display: flex;flex-direction: column;flex: 1;height: 24px;"><div style="flex: 1;display: flex;border-bottom: 1px solid #999999"><div id="' + data.id + 'toothBox1" class="leftUp" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox2" class="rightUp" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div><div style="flex: 1;display: flex"><div id="' + data.id + 'toothBox3" class="leftDown" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox4" class="rightDown" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div></div><div style="flex: 2;" class="beyond" title="' + data.payServiceName + '"><span class="payServiceName">' + data.payServiceName + '<span><input class="payServiceId" value="' + data.id + '" type="hidden" /><input class="vipPayserviceId" value="0" type="hidden" /></div><div><input autocomplete="off" oninput="decimalNum(this),updatePrice(this)" type="text" class="payService" name="payService" value="' + payServicePriceT + '"/><input class="discountHide" value="' + payServicePriceT + '" type="hidden"><input class="unitPrice" value="' + data.payServicePriceTwo + '" type="hidden"><input class="unitPriceOne" value="' + payServicePriceT + '" type="hidden"/><input class="unitPriceTwo" value="' + payServicePriceT + '" type="hidden"/><input class="priceRecord" value="' + payServicePriceT + '" type="hidden"/><input class="priceDiscountRecord" value="' + discount + '" type="hidden"/><input class="primary" value="' + payServicePriceTwo + '" type="hidden"></div><div style="flex: 0.5"><span>' + data.unit + '</span></div><div><a onclick="numReduce(this)" style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="reduce">-</a><input class="num" oninput="decimalNum(this),changeNum(this)" style="width:35px;vertical-align: top;" type="text" value="1"><a onclick="numAdd(this)" style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="add">+</a></div><div><input autocomplete="off" oninput="decimalNum(this),priceDiscount(this)" class="amount" value="' + payServicePriceTwo + '" type="text"/><input class="realAmount" value="' + payServicePriceTwo + '" type="hidden"/></div><div><input autocomplete="off" oninput="decimalNum(this),updateDiscount(this)" class="discount" value="' + discount + '" type="text"/></div><div><select onchange="updateDoctor(this)" class="doctor" style="width: 90%;">' + doctorHtml + '</select></div><div style="flex: 0.5"><a class="deleteDeal" onclick="deleteProject(this)">删除</a></div></div>';
                                    $("#projectListInfo").append(html);
                                } else {
                                    var discount = 100 * member;
                                    var payServicePriceTwo = parseFloat(data.payServicePriceTwo * member).toFixed(1);
                                    var html = '<div class="proDealInfo"><div id="' + data.id + 'toothBox" class="toothBox" style="display: flex;flex-direction: column;flex: 1;height: 24px;"><div style="flex: 1;display: flex;border-bottom: 1px solid #999999"><div id="' + data.id + 'toothBox1" class="leftUp" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox2" class="rightUp" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div><div style="flex: 1;display: flex"><div id="' + data.id + 'toothBox3" class="leftDown" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox4" class="rightDown" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div></div><div style="flex: 2;" class="beyond" title="' + data.payServiceName + '"><span class="payServiceName">' + data.payServiceName + '<span><input class="payServiceId" value="' + data.id + '" type="hidden" /><input class="vipPayserviceId" value="0" type="hidden" /></div><div><input autocomplete="off" oninput="decimalNum(this),updatePrice(this)" type="text" class="payService" name="payService" value="' + data.payServicePriceTwo + '"/><input class="discountHide" value="' + data.payServicePriceTwo + '" type="hidden"><input class="unitPrice" value="' + data.payServicePriceTwo + '" type="hidden"><input class="unitPriceOne" value="' + data.payServicePriceOne + '" type="hidden"/><input class="unitPriceTwo" value="' + data.payServicePriceTwo + '" type="hidden"/><input class="priceRecord" value="' + data.payServicePriceTwo + '" type="hidden"/><input class="priceDiscountRecord" value="' + discount + '" type="hidden"/><input class="primary" value="' + payServicePriceTwo + '" type="hidden"></div><div style="flex: 0.5"><span>' + data.unit + '</span></div><div><a onclick="numReduce(this)" style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="reduce">-</a><input class="num" oninput="decimalNum(this),changeNum(this)" style="width:35px;vertical-align: top;" type="text" value="1"><a onclick="numAdd(this)" style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="add">+</a></div><div><input autocomplete="off" oninput="decimalNum(this),priceDiscount(this)" class="amount" value="' + payServicePriceTwo + '" type="text"/><input class="realAmount" value="' + payServicePriceTwo + '" type="hidden"/></div><div><input autocomplete="off" oninput="decimalNum(this),updateDiscount(this)" class="discount" value="' + discount + '" type="text"/></div><div><select onchange="updateDoctor(this)" class="doctor" style="width: 90%;">' + doctorHtml + '</select></div><div style="flex: 0.5"><a class="deleteDeal" onclick="deleteProject(this)">删除</a></div></div>';
                                    $("#projectListInfo").append(html);
                                }
                            }
                            totalAmountMoney();
                        } else {
                            layer.msg('请不要选择重复的收费项目！', {
                                time: 1000,
                                icon: 2
                            })
                        }
                    }
                })
            }
        }
    })
}

function chooseProjectTwo(id, vipPayserviceId, obj) {
    var member = $("#discountDiscount").val();
    $.ajax({
        url: Feng.ctxPath + "/consultant/getCustomerStatus/" + $("#customerId").val(),
        dataType: "json",
        contentType: "application/json",
        type: "POST",
        success: function (data) {
            if (data.split(",")[0] == '复诊') {
                alert("复诊不能重新开单,请去进行重咨操作！");
                return
            } else if (data.split(",")[2] == 3) {
                alert("患者TC不是当前登录人，请联系前台分配患者！");
                return
            } else {
                $.ajax({
                    type: "POST",
                    url: Feng.ctxPath + "/payService/projectDetail",
                    data: {
                        id: id
                    },
                    dataType: "json",
                    success: function (data) {
                        var doctorHtml = document.getElementById('projectDoctor').innerHTML;
                        $(obj).parent().css({'pointer-events': 'none', 'background': '#F1F1F1'});
                        var dealList = $(".proDealInfo");
                        var isChoose = 0;
                        for (var i = 0; i < dealList.length; i++) {
                            if (data.id == dealList[i].payServiceId) {
                                isChoose = 1;
                            }
                        }
                        if (isChoose == 0) {
                            if (data.remarks == "a") {
                                var discount = 100 * member;
                                var payServicePriceTwo = parseFloat(data.payServicePriceTwo).toFixed(1);
                                var payServicePriceT = parseFloat(payServicePriceTwo / member).toFixed(1);
                                var html = '<div class="proDealInfo"><div id="' + data.id + 'toothBox" class="toothBox" style="display: flex;flex-direction: column;flex: 1;height: 24px;"><div style="flex: 1;display: flex;border-bottom: 1px solid #999999"><div id="' + data.id + 'toothBox1" class="leftUp" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox2" class="rightUp" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div><div style="flex: 1;display: flex"><div id="' + data.id + 'toothBox3" class="leftDown" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox4" class="rightDown" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div></div><div style="flex: 2;" class="beyond" title="' + data.payServiceName + '"><span class="payServiceName">' + data.payServiceName + '<span><input class="payServiceId" value="' + data.id + '" type="hidden" /><input class="vipPayserviceId" value="' + vipPayserviceId + '" type="hidden" /></div><div><input readonly="readonly" oninput="decimalNum(this),updatePrice(this)" type="text" class="payService" name="payService" value="0"/><input class="discountHide" value="0" type="hidden"><input class="unitPrice" value="0" type="hidden"><input class="unitPriceOne" value="0" type="hidden"/><input class="unitPriceTwo" value="0" type="hidden"/><input class="priceRecord" value="0" type="hidden"/><input class="priceDiscountRecord" value="100" type="hidden"/><input class="primary" value="0" type="hidden"></div><div><span>' + data.unit + '</span></div><div><a style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="reduce">-</a><input readonly="readonly" class="num" oninput="decimalNum(this),changeNum(this)" style="width:35px;vertical-align: top;" type="text" value="1"><a style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="add">+</a></div><div><input readonly="readonly" oninput="decimalNum(this),priceDiscount(this)" class="amount" value="0" type="text"/><input class="realAmount" value="0" type="hidden"/></div><div><input oninput="decimalNum(this),updateDiscount(this)" class="discount" value="100" type="text"/></div><div><select onchange="updateDoctor(this)" class="doctor" style="width: 90%;">' + doctorHtml + '</select></div><div><a class="deleteDeal" onclick="deleteProject(this,\'' + vipPayserviceId + '\')">删除</a></div></div>';
                                $("#projectListInfo").append(html);
                            } else {
                                var discount = 100 * member;
                                var payServicePriceTwo = parseFloat(data.payServicePriceTwo * member).toFixed(1);
                                var html = '<div class="proDealInfo"><div id="' + data.id + 'toothBox" class="toothBox" style="display: flex;flex-direction: column;flex: 1;height: 24px;"><div style="flex: 1;display: flex;border-bottom: 1px solid #999999"><div id="' + data.id + 'toothBox1" class="leftUp" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox2" class="rightUp" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div><div style="flex: 1;display: flex"><div id="' + data.id + 'toothBox3" class="leftDown" style="flex: 1;border-right: 1px solid #999999;line-height: 11px;text-align: right;padding-right: 5px;"></div><div id="' + data.id + 'toothBox4" class="rightDown" style="flex: 1;line-height: 11px;padding-left: 5px;"></div></div></div><div style="flex: 2;" class="beyond" title="' + data.payServiceName + '"><span class="payServiceName">' + data.payServiceName + '<span><input class="payServiceId" value="' + data.id + '" type="hidden" /><input class="vipPayserviceId" value="' + vipPayserviceId + '" type="hidden" /></div><div><input readonly="readonly" oninput="decimalNum(this),updatePrice(this)" type="text" class="payService" name="payService" value="0"/><input class="discountHide" value="0" type="hidden"><input class="unitPrice" value="0" type="hidden"><input class="unitPriceOne" value="0" type="hidden"/><input class="unitPriceTwo" value="0" type="hidden"/><input class="priceRecord" value="0" type="hidden"/><input class="priceDiscountRecord" value="100" type="hidden"/><input class="primary" value="0" type="hidden"></div><div><span>' + data.unit + '</span></div><div><a style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="reduce">-</a><input readonly="readonly" class="num" oninput="decimalNum(this),changeNum(this)" style="width:35px;vertical-align: top;" type="text" value="1"><a style="border:1px solid  #999999;width: 17px;height: 22px;display: inline-block;" class="add">+</a></div><div><input readonly="readonly" oninput="decimalNum(this),priceDiscount(this)" class="amount" value="0" type="text"/><input class="realAmount" value="0" type="hidden"/></div><div><input oninput="decimalNum(this),updateDiscount(this)" class="discount" value="100" type="text"/></div><div><select onchange="updateDoctor(this)" class="doctor" style="width: 90%;">' + doctorHtml + '</select></div><div><a class="deleteDeal" onclick="deleteProject(this,\'' + vipPayserviceId + '\')">删除</a></div></div>';
                                $("#projectListInfo").append(html);
                            }
                            totalAmountMoney();
                        } else {
                            layer.msg('请不要选择重复的收费项目！', {
                                time: 1000,
                                icon: 2
                            })
                        }
                    }
                })
            }
        }
    })
}

function updateDoctor(obj) {
    $("#projectDoctor").val("")
}

function chooseDoctor() {
    var proDoctor = $("#projectDoctor").val();
    $(".doctor").val(proDoctor)
}

function decimalNum(obj) {
    obj.value = obj.value.replace(/[^\d.]/g, "");
    obj.value = obj.value.replace(/^\./g, "");
    obj.value = obj.value.replace(/\.{2,}/g, ".");
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
    if (obj.value.indexOf(".") < 0 && obj.value != "") {
        obj.value = parseFloat(obj.value)
    }
}

function deleteProject(obj, id) {
    $("#" + id).parent().css({'pointer-events': 'auto', 'background': 'white'});
    var project = $(obj).parent().parent();
    $(project).remove();
    var proDealList = $(".proDealInfo");
    if (proDealList.length == 0) {
        $("#totalSum1").val(0);
        $("#realTotalSum").val(0);
        $("#originalPrice").val(0);
        $("#newTotalSum").val(0);
        $("#newPayAmount").val(0);
        $("#totalDiscount").val(100)
    } else {
        totalAmountMoney()
    }
};

function updatePrice(obj) {
    var price = parseFloat($(obj).val());
    var unitPrice = parseFloat($(obj).parent().find(".unitPrice").val());
    var discount = parseFloat($(obj).parent().parent().find(".discount").val()) / 100;
    var priceOne = $(obj).parent().find(".unitPriceOne").val();
    var unitPriceTwo = $(obj).parent().find(".unitPriceTwo").val();
    var num = parseInt($(obj).parent().parent().find(".num").val());
    if (priceOne == unitPrice) {
        $(obj).val(unitPrice)
    } else {
        if (isNaN(price)) {
            $(obj).val(0);
            $(obj).parent().parent().find(".amount").val(0);
            $(obj).parent().parent().find(".realAmount").val(0);
            totalAmountMoney()
        } else {
            if (price <= unitPriceTwo && price >= 0) {
                if (num > 0) {
                    $(obj).parent().parent().find(".priceRecord").val(price);
                    var amount = parseFloat(price * num * discount).toFixed(2);
                    $(obj).parent().parent().find(".priceDiscountRecord").val(discount);
                    $(obj).parent().parent().find(".amount").val(amount);
                    $(obj).parent().parent().find(".realAmount").val(amount);
                    $(obj).parent().parent().find(".primary").val(amount)
                }
                totalAmountMoney()
            } else {
                $(obj).val($(obj).parent().parent().find(".priceRecord").val())
            }
        }
    }
};

function priceDiscount(obj) {
    var amount = parseFloat($(obj).val());
    var payService = $(obj).parent().parent().find(".payService").val();
    var num = $(obj).parent().parent().find(".num").val();
    if (isNaN(amount)) {
        $(obj).val(0);
        $(obj).parent().parent().find(".discount").val(0);
        totalAmountMoney()
    } else {
        if (amount > (num * payService)) {
            $(obj).val($(obj).parent().parent().find(".primary").val())
        } else {
            $(obj).parent().parent().find(".primary").val(amount);
            $(obj).parent().parent().find(".realAmount").val(amount);
            var discount = parseFloat(amount / (num * payService));
            $(obj).parent().parent().find(".discount").val(parseFloat(discount * 100).toFixed(2));
            $(obj).parent().parent().find(".priceDiscountRecord").val(parseFloat(discount * 100).toFixed(2));
            totalAmountMoney()
        }
    }
}

function changeNum(obj) {
    var num = parseInt($(obj).val());
    var priceRecord = parseFloat($(obj).parent().parent().find(".priceRecord").val());
    var discount = parseFloat($(obj).parent().parent().find(".discount").val()) / 100;
    if (isNaN(num)) {
        $(obj).val(0);
        $(obj).parent().parent().find(".amount").val(0);
        $(obj).parent().parent().find(".realAmount").val(0);
        totalAmountMoney()
    } else {
        var amount = parseFloat(priceRecord * num * discount).toFixed(1);
        $(obj).parent().parent().find(".amount").val(amount);
        $(obj).parent().parent().find(".realAmount").val(amount);
        $(obj).parent().parent().find(".priceDiscountRecord").val(discount);
        var payService = parseFloat($(obj).parent().parent().find(".payService").val());
        $(obj).parent().parent().find(".primary").val(amount)
    }
    totalAmountMoney()
}

function numReduce(obj) {
    var parentNode = $(obj).parent();
    var num = $(parentNode).parent().find(".num").val();
    var unitPrice = $(parentNode).parent().find(".payService").val();
    var priceRecord = $(obj).parent().parent().find(".priceRecord").val();
    var discount = parseFloat($(obj).parent().parent().find(".discount").val()) / 100;
    if (parseInt(num) > 1) {
        $(parentNode).parent().find(".num").val(parseInt(num) - 1);
        var count = parseInt(num) - 1;
        var amount = parseFloat(priceRecord * count * discount).toFixed(1);
        $(obj).parent().parent().find(".priceDiscountRecord").val(discount);
        $(parentNode).parent().find(".amount").val(amount);
        $(parentNode).parent().find(".realAmount").val(amount);
        $(parentNode).parent().find(".primary").val(amount);
        totalAmountMoney()
    }
}

function numAdd(obj) {
    var parentNode = $(obj).parent();
    var num = $(parentNode).parent().find(".num").val();
    var discountHide = $(parentNode).parent().find(".discountHide").val();
    var unitPrice = $(parentNode).parent().find(".payService").val();
    var priceRecord = $(obj).parent().parent().find(".priceRecord").val();
    var discount = parseFloat($(obj).parent().parent().find(".discount").val()) / 100;
    $(parentNode).parent().find(".num").val(parseInt(num) + 1);
    var count = parseInt(num) + 1;
    var amount = parseFloat(priceRecord * count * discount).toFixed(1);
    $(parentNode).parent().find(".amount").val(amount);
    $(obj).parent().parent().find(".priceDiscountRecord").val(discount);
    $(parentNode).parent().find(".realAmount").val(amount);
    $(parentNode).parent().find(".primary").val(amount);
    totalAmountMoney()
}

function updateDiscount(obj) {
    var discount = parseFloat($(obj).val()) / 100;
    var payService = $(obj).parent().parent().find(".payService").val();
    if (isNaN(discount)) {
        $(obj).val(0);
        $(obj).parent().parent().find(".amount").val(0);
        $(obj).parent().parent().find(".realAmount").val(0);
        totalAmountMoney()
    } else {
        if ($(obj).val() > 100) {
            $(obj).val($(obj).parent().parent().find(".priceDiscountRecord").val());
            totalAmountMoney()
        } else {
            $(obj).parent().parent().find(".priceDiscountRecord").val($(obj).val());
            if (discount >= 0) {
                var num = $(obj).parent().parent().find(".num").val();
                var price = parseFloat(payService);
                $(obj).parent().parent().find(".discountHide").val(price);
                var amount = parseFloat(num * price * discount).toFixed(2);
                $(obj).parent().parent().find(".amount").val(amount);
                $(obj).parent().parent().find(".realAmount").val(amount);
                $(obj).parent().parent().find(".primary").val(amount);
                totalAmountMoney()
            }
        }
    }
}

function totalAmountMoney() {
    var totalSum = 0;
    $("#projectListInfo .priceRecord").each(function (index, obj) {
        totalSum += parseFloat($(obj).val() * $(obj).parent().parent().find(".num").val())
    });
    var whole = 0;
    $("#projectListInfo .primary").each(function (index, obj) {
        whole += parseFloat($(obj).val())
    });
    $("#totalSum1").val(whole.toFixed(2));
    $("#originalPrice").val(totalSum.toFixed(2));
    $("#newTotalSum").val(whole.toFixed(2));
    $("#newPayAmount").val(whole.toFixed(2));
    var wholeDiscount = parseFloat(whole / totalSum).toFixed(2);
    if (isNaN(wholeDiscount)) {
        $("#totalDiscount").val(100.00)
    } else {
        $("#totalDiscount").val(wholeDiscount * 100)
    }
    $("#newArrears").val(parseFloat($("#newTotalSum").val()) - parseFloat($("#newPayAmount").val()))
}

inputNum = function () {
    var input = document.getElementsByClassName('totleinput');
    var inputNum = 0;
    for (var i = 0; i < input.length; i++) {
        inputNum += parseFloat(input[i].value)
    }
    $("#serviceActualPay").attr("value", inputNum)
};